package com.assessment.controller;

import com.assessment.model.entity.AssessmentPeriod;
import com.assessment.service.AssessmentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

/**
 * H5页面控制器
 */
@Controller
@RequestMapping("/h5")
public class H5Controller {

    @Autowired
    private AssessmentService assessmentService;

    /**
     * 登录页
     */
    @GetMapping("/login")
    public String login() {
        return "login";
    }

    /**
     * 首页
     */
    @GetMapping("/index")
    public String index(Model model) {
        try {
            // 获取当前评测周期
            AssessmentPeriod currentPeriod = assessmentService.getCurrentPeriod();
            if (currentPeriod != null) {
                // 格式化时间
                currentPeriod.formatTime();
            }
            model.addAttribute("currentPeriod", currentPeriod);
        } catch (Exception e) {
            // 即使获取周期失败，也继续处理请求
            model.addAttribute("currentPeriod", null);
        }
        return "index";
    }

    /**
     * 评测任务页
     */
    @GetMapping("/tasks")
    public String tasks(Model model) {
        try {
            // 获取当前评测周期
            AssessmentPeriod currentPeriod = assessmentService.getCurrentPeriod();
            if (currentPeriod != null) {
                // 格式化时间
                currentPeriod.formatTime();
            }
            model.addAttribute("currentPeriod", currentPeriod);
        } catch (Exception e) {
            // 即使获取周期失败，也继续处理请求
            model.addAttribute("currentPeriod", null);
        }
        return "tasks";
    }

    /**
     * 评测详情页
     */
    @GetMapping("/task/detail")
    public String taskDetail(@RequestParam Integer relationId, Model model) {
        try {
            // 获取当前评测周期
            AssessmentPeriod currentPeriod = assessmentService.getCurrentPeriod();
            if (currentPeriod != null) {
                // 格式化时间
                currentPeriod.formatTime();
            }
            model.addAttribute("currentPeriod", currentPeriod);
        } catch (Exception e) {
            // 即使获取周期失败，也继续处理请求
            model.addAttribute("currentPeriod", null);
        }
        model.addAttribute("relationId", relationId);
        return "task_detail";
    }

    /**
     * 评测结果页
     */
    @GetMapping("/result")
    public String result(Model model) {
        try {
            // 获取当前评测周期
            AssessmentPeriod currentPeriod = assessmentService.getCurrentPeriod();
            if (currentPeriod != null) {
                // 格式化时间
                currentPeriod.formatTime();
            }
            model.addAttribute("currentPeriod", currentPeriod);
        } catch (Exception e) {
            // 即使获取周期失败，也继续处理请求
            model.addAttribute("currentPeriod", null);
        }
        return "result";
    }

    /**
     * 个人信息页
     */
    @GetMapping("/my")
    public String my(Model model) {
        try {
            // 获取当前评测周期
            AssessmentPeriod currentPeriod = assessmentService.getCurrentPeriod();
            if (currentPeriod != null) {
                // 格式化时间
                currentPeriod.formatTime();
            }
            model.addAttribute("currentPeriod", currentPeriod);
        } catch (Exception e) {
            // 即使获取周期失败，也继续处理请求
            model.addAttribute("currentPeriod", null);
        }
        return "my";
    }
}
